package com.jgzx.utils;

import cn.hutool.core.util.StrUtil;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;

/**
 * 平台门户加解密工具类
 * @author libo
 * @date 2021/7/29 16:33
 */
public class PortalEncryptUtil {

    /**
     * 密钥
     */
    private static final String KEY = "cqanjian_xt@2017";

    /**
     * 向量
     */
    private static final String IV = "caacetc.com@2017";

    /**
     * 算法
     */
    private static final String ALGORITHMSTR = "AES/CBC/PKCS5Padding";

    /**
     * aes 解密
     * @param encrypt 内容
     * @return 解密后的字符串
     */
    public static String aesDecrypt(String encrypt) throws Exception {
        return aesDecrypt(encrypt, KEY,IV);
    }

    /**
     * aes 加密
     * @param content 待加密内容
     * @param argIv 向量
     * @return 加密后的内容
     */
    public static String aesEncrypt(String content, String argIv) throws Exception {
        return aesEncrypt(content, KEY, argIv);
    }

    public static String aesEncrypt(String content) throws Exception {
        return aesEncrypt(content, KEY, IV);
    }
    /**
     * 将 byte[]转为各种进制的字符串
     * @param bytes byte[]
     * @param radix 可以转换进制的范围，从 Character.MIN_RADIX 到 Character.MAX_RADIX，超出范围后变为 10 进制
     * @return 转换后的字符串
     */
    public static String binary(byte[] bytes, int radix){
        return new BigInteger(1, bytes).toString(radix);// 这里的 1 代表正数
    }
    /**
     * base 64 encode
     * @param bytes 待编码的 byte[]
     * @return 编码后的 base 64 code
     */
    public static String base64Encode(byte[] bytes){
        return Base64.encodeBase64String(bytes);
    }
    /**
     * base 64 decode
     * @param base64Code 待解码的 base 64 code
     * @return 解码后的 byte[]
     */
    public static byte[] base64Decode(String base64Code) throws Exception{
        return StrUtil.isEmpty(base64Code) ? null : Base64.decodeBase64(base64Code);
    }

    /**精耕咨询 版权所有 23 /5
     * AES 加密
     * @param content 待加密的内容
     * @param encryptKey 加密密钥
     * @return 加密后的 byte[]
     */
    public static byte[] aesEncryptToBytes(String content, String encryptKey,String argIv) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
        // cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(),"AES"));
        IvParameterSpec iv = new IvParameterSpec(argIv.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), "AES"), iv);
        return cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
    }
    /**
     * AES 加密为 base 64 code
     * @param content 待加密的内容
     * @param encryptKey 加密密钥
     * @return 加密后的 base 64 code
     */
    public static String aesEncrypt(String content, String encryptKey,String argIv) throws Exception {
        return base64Encode(aesEncryptToBytes(content, encryptKey, argIv));
    }
    /**
     * AES 解密
     * @param encryptBytes 待解密的 byte[]
     * @param decryptKey 解密密钥
     * @return 解密后的 String
     */
    public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey,String argIv) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
        // cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), "AES"));
        IvParameterSpec iv = new IvParameterSpec(argIv.getBytes());
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), "AES"), iv);
        byte[] decryptBytes = cipher.doFinal(encryptBytes);
        return new String(decryptBytes, StandardCharsets.UTF_8);
    }
    /**
     * 将 base 64 code AES 解密
     * @param encryptStr 待解密的 base 64 code
     * @param decryptKey 解密密钥
     * @return 解密后的 string
     */
    public static String aesDecrypt(String encryptStr, String decryptKey,String argIv) throws Exception {
        return StrUtil.isEmpty(encryptStr) ? null : aesDecryptByBytes(base64Decode(encryptStr), decryptKey, argIv);
    }

    public static void main(String[] args) throws Exception {
        String content = "{\n" +
                "  \"appId\": \"123\",\n" +
                "  \"beginDate\": \"\",\n" +
                "  \"businesslicense\": \"\",\n" +
                "  \"contactsmailbox\": \"\",\n" +
                "  \"contactsname\": \"\",\n" +
                "  \"contactstel\": \"\",\n" +
                "  \"endDate\": \"\",\n" +
                "  \"enterpriseareacode\": \"打的\",\n" +
                "  \"enterpriseareaname\": \"312321\",\n" +
                "  \"enterprisecitycode\": \"31232\",\n" +
                "  \"enterprisecityname\": \"312321\",\n" +
                "  \"enterprisecode\": \"发滚滚滚滚滚\",\n" +
                "  \"enterprisedetails\": \"\",\n" +
                "  \"enterpriseindustry\": \"\",\n" +
                "  \"enterpriseintroduce\": \"\",\n" +
                "  \"enterprisemailbox\": \"\",\n" +
                "  \"enterprisename\": \"\",\n" +
                "  \"enterpriseprovicecode\": \"\",\n" +
                "  \"enterpriseprovicename\": \"\",\n" +
                "  \"enterprisetype\": \"\",\n" +
                "  \"enterprisewebsite\": \"\",\n" +
                "  \"id\": \"\",\n" +
                "  \"isprefix\": \"\",\n" +
                "  \"moduleAttribute\": \"\",\n" +
                "  \"personliablename\": \"\",\n" +
                "  \"personliablenumber\": \"\",\n" +
                "  \"personliablephoto\": \"\",\n" +
                "  \"personliablephoto2\": \"\",\n" +
                "  \"personliabletype\": \"\",\n" +
                "  \"registeraddress\": \"\",\n" +
                "  \"renewaltime\": \"\",\n" +
                "  \"tenantId\": \"\"\n" +
                "}";
        System.out.println("加密前： " + content);
        System.out.println("加密密钥： " + KEY);
        System.out.println("加密 IV： " + IV);
        String encrypt = aesEncrypt(content, KEY, IV);
        System.out.println("加密后： " + encrypt);
    }
}
